package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.common.entity.pojo.vo.DateResult;
import com.tbit.uqbike.object.pojo.statistics.ExportPowerLog;
import com.tbit.uqbike.object.pojo.statistics.MoveLog;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.MoveLogService;
import com.tbit.uqbike.webmanager.util.PowerLogExportUtil;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelUtil;
import com.tbit.uqbike.webmanager.util.excel.ExportExcelWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: chen
 * @Description: 挪车统计
 * @Date: 创建于 17:45 2020/3/13
 */
@Validated
@RestController
@RequestMapping(value = "/moveLogStat", produces = BaseController.MEDIA_JSON_UTF8)
public class MoveLogController extends BaseController {
    @Autowired
    private MoveLogService moveLogService;

    /**
     * 柱状图数据
     */
    @RequestMapping("/statisticsByTime")
    public JsonResult statisticsByTime(@NotNull(message = "ver.accountId.notNull") Integer accountId, Integer accountUserId, @NotNull(message = "ver.startTime.notNull") Long startTime,
                                       @NotNull(message = "ver.endTime.notNull") Long endTime) {
        List<DateResult> list = moveLogService.statisticsByTime(accountId, accountUserId, startTime, endTime);
        return JsonResult.ok(list);
    }

    /**
     * 分页查询详细
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(@NotNull(message = "ver.accountId.notNull") Integer accountId, String machineNO, String userCode,
                                Integer accountUserId, Long startTime, Long endTime, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {

        List<MoveLog> list = moveLogService.queryPage(accountId, machineNO, userCode, accountUserId, startTime, endTime, pageNO, rowCount);
        Integer total = moveLogService.queryCount(accountId, machineNO, userCode, accountUserId, startTime, endTime);
        return JsonResult.ok(list, total);
    }

    /**
     * 导出
     */
    @RequestMapping("/exportByTime")
    public void exportByTime(@NotNull(message = "ver.accountId.notNull") Integer accountId, Integer accountUserId, @NotNull(message = "ver.startTime.notNull") Long startTime,
                             @NotNull(message = "ver.endTime.notNull") Long endTime, HttpServletResponse response) {
//        if(null==accountUserId){
//            throw new BaseException(SystemCodeEnum.SELECT_OPERATION_AND_MAINTENANCE_PERSONNEL);
//        }
        RequestContext context = getLogin(ServletUtils.getRequest()).getContext();
        List<MoveLog> list = moveLogService.exportByTime(accountId, accountUserId, startTime, endTime);
        List<String[]> data = PowerLogExportUtil.getExportContent(null, list, null, null, context);
        ExportExcelWrapper<String[]> export = new ExportExcelWrapper<>();
        export.exportExcel(context.getMessage("excel_head_move"), context.getMessage("excel_head_move"), null, data, response, ExportExcelUtil.EXCEL_FILE_2003);
    }

    /**
     * 挪车总详情
     */
    @RequestMapping("/getByType")
    public JsonResult getByType(@NotNull(message = "ver.accountId.notNull") Integer accountId, Integer accountUserId, @NotNull(message = "ver.startTime.notNull") Long startTime,
                                @NotNull(message = "ver.endTime.notNull") Long endTime, @RequestParam(defaultValue = "10") Integer rowCount, @RequestParam(defaultValue = "1") Integer pageNO, HttpServletResponse response) {
        List<ExportPowerLog> list = moveLogService.getByType(new HashMap<String, Object>(), accountId, accountUserId, startTime, endTime, rowCount, pageNO);
        PageInfo pageInfo = new PageInfo(list);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

}
